//
//  NewFeatureViewController.swift
//  sina
//
//  Created by DXapple on 15/11/14.
//  Copyright © 2015年 sqlite. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"
private let imageCount = 4
class NewFeatureViewController: UICollectionViewController {

    //重写集合视图控制器的构造方法
    /*
    1.让外界调用更加简单  不需要传参数
    2.layout 和视图的大小比较固定
    */

    init() {
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.scrollDirection = .Horizontal
    super.init(collectionViewLayout: layout)
    //设置分页滚动  必须在super之后写不然不能用
        collectionView?.pagingEnabled = true
        collectionView?.showsHorizontalScrollIndicator  = false
        collectionView?.bounces = false
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        
        self.collectionView!.registerClass(NewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)

    }

    

    // MARK: UICollectionViewDataSource

    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        return imageCount
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! NewFeatureCell

        cell.index = indexPath.item
        return cell
    }
    //停止滚动协议方法
    
    
    
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {

        
        let page = Int(scrollView.contentOffset.x / screenW)
        print(page)

        if page == imageCount - 1 {
            
            if let  cell = collectionView?.visibleCells().last as? NewFeatureCell {
                cell.startAnimation()
//                cell.startButton.hidden = false

            }
        }
    }
}
class NewFeatureCell: UICollectionViewCell {
        var index: Int = 0 {
        didSet {
        iconImage.image = UIImage(named: "new_feature_\(index + 1)")
            startButton.hidden = true
            
//            if index == 3 {
//                self.performSelector(<#T##aSelector: Selector##Selector#>)
//              startAnimation()
//                
//            }
        }
            

    }
    
  @objc  private func start() {
    
     print("点击了按钮")
    
    NSNotificationCenter.defaultCenter().postNotificationName(AppSwitchRootViewControler , object: nil)
    }
    
    private func startAnimation() {
        startButton.transform = CGAffineTransformMakeScale(0, 0)
        UIView.animateWithDuration(2, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: [], animations: { () -> Void in
            self.startButton.hidden = false

            self.startButton.transform = CGAffineTransformMakeScale(1, 1)
        }) { (_) -> Void in
            print("ok")

        }

    
    }
        
    override init(frame: CGRect) {
        super.init(frame: frame)
//        print(frame)
        setUI()
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //2布局
    private func setUI() {
    //1添加子视图
        contentView.addSubview(iconImage)
        contentView.addSubview(startButton)
    
        //edges 表示对于四周的约束
        iconImage.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView.snp_edges)
            
        }
        
        startButton.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(contentView.snp_centerX)
            make.bottom.equalTo(contentView.snp_bottom).offset(-200)
        }
        startButton.hidden = true
        //btn
        startButton.addTarget(self, action: "start", forControlEvents: .TouchUpInside)
        
        
    }
    
    //1 实例化子视图控件
    private lazy var iconImage: UIImageView = UIImageView()

    private lazy var startButton: UIButton = UIButton(title: "开始体检", backImage: "new_feature_finish_button", color: nil)

    
//    private lazy var startButton : UIButton = {
//        let btn = UIButton()
//        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: .Normal)
//        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: .Highlighted)
//        
//        btn.setTitle("开始体检", forState: .Normal)
//        
//        return btn
//        }()
    
}







